package com.touhou.youmu.api

import com.touhou.youmu.mapper.ApiMapper
import com.touhou.youmu.utils.FileUtils
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiOperation
import org.springframework.web.bind.annotation.RestController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.multipart.MultipartFile
import javax.servlet.http.HttpServletRequest


@RestController
class Api {
    @RequestMapping("/")
    fun home() = "api服务运行中。。。"
    @Value(value = "\${my.filePath}")
    private var filePath: String? = null
    @Autowired
    lateinit var apiMapper: ApiMapper

    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/regUser",method= [(RequestMethod.POST)])
    fun regUser(username:String,password:String):Int{
        return apiMapper?.addTest(mapOf("id" to FileUtils.getUUID(),"username" to username,"password" to password))
    }

    @ApiOperation(value = "用户登陆")
    @RequestMapping(value = "/loginUser",method= [(RequestMethod.POST)])
    fun loginUser(username:String,password:String):List<Map<String,String>>{
        return apiMapper?.loginUser(mapOf("username" to username,"password" to password))
    }

    @ApiOperation(value = "查询书籍")
    @RequestMapping(value = "/slectsj",method= [(RequestMethod.POST)])
    fun slectsj():List<Map<String,String>>{
        return apiMapper?.slectys()
    }
    @ApiOperation(value = "搜索书籍")
    @RequestMapping(value = "/slectsssj",method= [(RequestMethod.POST)])
    fun slectsssj(bookname:String):List<Map<String,String>>{
        return apiMapper?.slectsssj(mapOf("bookname" to bookname))
    }

    @ApiOperation(value = "查询自己书籍")
    @RequestMapping(value = "/slectsssjzj",method= [(RequestMethod.POST)])
    fun slectsssjzj(uid:String):List<Map<String,String>>{
        return apiMapper?.slectsssj2(mapOf("uid" to uid))
    }

    @ApiOperation(value = "修改书籍")
    @RequestMapping(value = "/updateSj",method= [(RequestMethod.POST)])
    fun updateSj(id:String, bookname: String,bookcb:String,bookzz:String ):Int{

        return  apiMapper?.updatebook(mapOf("bookname" to bookname,"bookcb" to id,"bookcb" to id,"bookzz" to bookzz))
    }


    @ApiOperation(value = "删除书籍")
    @RequestMapping(value = "/deletSJ",method= [(RequestMethod.POST)])
    fun deletSJ(id:String):Int{
        return apiMapper?.deletSJ(mapOf("id" to id))
    }

//    @ApiOperation(value = "找回密码")
//    @RequestMapping(value = "/findPassword",method= [(RequestMethod.POST)])
//    fun findPassword(wenti:String,huida:String):List<Map<String,String>>{
//        return apiMapper?.findPassword(mapOf("wenti" to wenti,"huida" to huida))
//    }

    @ApiOperation(value = "发布书籍")
    @RequestMapping(value = "/saveYSXX",method= [(RequestMethod.POST)])
    fun saveYSXX(@RequestParam("file") file: MultipartFile, request: HttpServletRequest,uid: String,bookname: String,bookcb: String,bookzz: String,jf: String ):String{

        // 文件类型
        val contentType = file.contentType
        // 文件名
        val fileNames = file.originalFilename
        // 替换文件名
        val fileName = fileNames?.replace(fileNames?.substring(0,fileNames?.lastIndexOf('.')),FileUtils.getRandomFileName())
        // 文件地址
        val filePaths = filePath?.substring(6)

        try {
            if (fileName != null) {
                if (filePaths != null) {
                    FileUtils.uploadFile(file.bytes, filePaths, fileName)
                }
            }
        } catch (e: Exception) {
            return "error"
        }
        var myjf = jf.toInt() + 2
        apiMapper?.updatejf(mapOf("id" to uid,"jf" to myjf.toString()))
        apiMapper?.saveYSXX(mapOf("id" to FileUtils.getUUID(),"upid" to uid,"bookname" to bookname,"bookimg" to  "/file/"+fileName,"bookcb" to bookcb,"bookzz" to bookzz) as Map<String, String>)
        return "ok"
    }


}